package com.example.math_exam.MVC.acttivity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.math_exam.MVC.BaseActivity;
import com.example.math_exam.R;
import com.example.math_exam.ShopAdapter;
import com.example.math_exam.ShopEntitiy;

import java.util.ArrayList;
import java.util.List;

public class GwdActivity extends BaseActivity implements View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {

    private ArrayList<ShopEntitiy> list;

    private TextView mGwdBianJi;
    private Button mGwcJs;
    private ImageView mGwdBack;
    private RecyclerView mGwdRecycler;
    private ShopAdapter adapter;
    private TextView gwdPrice;
    private boolean isShow;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_gwd;
    }

    @Override
    protected void initActivity() {

        mGwdBianJi = f(R.id.gwd_bianJi);
        mGwcJs = f(R.id.gwc_js);
        mGwdBack = f(R.id.gwd_back);
        gwdPrice = f(R.id.gwd_allPrice);
        mGwdRecycler = f(R.id.gwd_recycler);
        adapter = new ShopAdapter(R.layout.item_shop_list);
        isShow = false;
        list = new ArrayList<>();
    }

    @Override
    protected void setOperation() {
        ShopEntitiy shopEntitiy = (ShopEntitiy) getIntent().getSerializableExtra("shop");
        list.add( shopEntitiy);

        mGwdRecycler.setLayoutManager( new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mGwdRecycler.setAdapter( adapter);

        adapter.setNewData( list);

        getPrice();
    }

    @Override
    protected void setListener() {

        mGwdBianJi.setOnClickListener(this);
        mGwdBack.setOnClickListener(this);
        mGwcJs.setOnClickListener(this);

        adapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gwd_bianJi:

                isShow = !isShow;

                for (ShopEntitiy shopEntitiy : list) {
                    shopEntitiy.setDelete( isShow);
                }

                adapter.notifyDataSetChanged();
                break;
            case R.id.gwc_js:

                Intent intent = new Intent(this, EnterActivity.class);
                intent.putExtra( "list", list);
                startClass(intent);
                break;
            case R.id.gwd_back:
                finish();
                break;
        }
    }

    private void getPrice() {

        List<ShopEntitiy> list= adapter.getData();

        if( list == null || list.size() < 1) return;

        float price = 0;

        for (ShopEntitiy shopEntitiy : list) {
            price = (float) (shopEntitiy.getNum() * shopEntitiy.getPrice());
        }

        Log.e("ghd", "getPrice: " + price );

        gwdPrice.setText("总金额：" + price);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        Log.e("ghd", "onItemChildClick: " + position );
        switch (view.getId()) {
            case R.id.item_shop_jia:
                ShopEntitiy jia = this.adapter.getItem(position);
                jia.setNum(  jia.getNum() + 1);
                Log.e("hgd", "onItemChildClick: jia" );
                break;
            case R.id.item_shop_jian:
                ShopEntitiy jian = this.adapter.getItem(position);
                jian.setNum( jian.getNum() - 1);
                Log.e("hgd", "onItemChildClick: jian" );
                break;
            case R.id.item_shop_delete:
                this.adapter.remove(position);
                break;
        }

        this.adapter.notifyDataSetChanged();
        getPrice();
    }
}
